package com.dingding.webservice.starlevelinfo.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.dingding.bean.databean.request.ReqGetDriverInfoAndLevel;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResGetDriverInfoLevel;
import com.dingding.common.core.base.dto.DubboInvokResponseDTO;
import com.dingding.common.core.beans.StarLevelInfo;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.evaluate.GetDriverInfoDTO;
import com.dingding.service.BaseService;
import com.dingding.webservice.starlevelinfo.IStarLevelInfoService;

/**
 * @ClassName: StarLevelInfoServiceImpl
 * @Description: 星级等级信息Service实现类
 * @author YHQ
 * @date 2016年5月12日 下午3:18:41
 */
public class StarLevelInfoServiceImpl extends BaseService implements IStarLevelInfoService {

	private Logger log = Logger.getLogger(StarLevelInfoServiceImpl.class);

	/**
	 * @Title: getStarLevelInfos
	 * @Description: 获取星级等级信息
	 * @param dataResponse
	 *            响应客户端请求Bean
	 * @param reqGetDriverInfoAndLevel
	 *            数据请求bean
	 * @author YHQ
	 * @return void 返回类型
	 */
	@Override
	public void getStarLevelInfos(ReqGetDriverInfoAndLevel reqGetDriverInfoAndLevel, DataResponse dataResponse) {

		if (reqGetDriverInfoAndLevel == null 
				|| ToolUtil.isBlank(reqGetDriverInfoAndLevel.getOrderId())
				|| reqGetDriverInfoAndLevel.getRegistrationType() == null) {
			log.error("客户端请求数据为空！");
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 获取星级等级所对应的评价标签
		List<StarLevelInfo> starLevelInfos = super.starLevelInfoFacade.getStarLevelInfo();

		// 由于用户评价需要显示车主信息、订单相关信息，为防止多次http请求，所以将查询次信息的相关代码直接写此接口中，并没有另开接口
		// 获取用户评价所需要的车主信息、订单信息
		DubboInvokResponseDTO<GetDriverInfoDTO> driverInfoDTOs = super.userInfoFacade
				.getDriverInfo_And_OrderInfo(reqGetDriverInfoAndLevel.getOrderId(),reqGetDriverInfoAndLevel.getRegistrationType());

		ResGetDriverInfoLevel resGetDriverInfoLevel = converBean(driverInfoDTOs, starLevelInfos);

		dataResponse.setResponseBody(resGetDriverInfoLevel);

		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	private ResGetDriverInfoLevel converBean(DubboInvokResponseDTO<GetDriverInfoDTO> driverInfoDTOs,
			List<StarLevelInfo> starLevelInfos) {

		ResGetDriverInfoLevel resGetDriverInfoLevel = new ResGetDriverInfoLevel();

		if (driverInfoDTOs != null) {
			if (driverInfoDTOs.getStatus()) {
				GetDriverInfoDTO getDriverInfoDTO = driverInfoDTOs.getResponseData();
				if (getDriverInfoDTO != null) {
					resGetDriverInfoLevel.setCreateTime(getDriverInfoDTO.getCreateTime()); // 创建时间
					resGetDriverInfoLevel.setTotalMoney(ToolUtil.keepOnePlaces(getDriverInfoDTO.getTotalMoney())); // 订单总额
					resGetDriverInfoLevel.setNickName(getDriverInfoDTO.getNickName());// 用户昵称
					resGetDriverInfoLevel.setUserAvatar(getDriverInfoDTO.getUserAvatar());// 用户头像
					resGetDriverInfoLevel.setVehicleModel(getDriverInfoDTO.getVehicleModel()); // 车辆品牌
					resGetDriverInfoLevel.setVehicleColor(getDriverInfoDTO.getVehicleColor());// 车辆颜色
					resGetDriverInfoLevel.setLisencePlate(getDriverInfoDTO.getLisencePlate()); // 车牌
					resGetDriverInfoLevel.setCreditLevel(getDriverInfoDTO.getCreditLevel()); // 车主信誉等级
					resGetDriverInfoLevel.setStarLevelInfos(starLevelInfos);
				}
			}
		}

		return resGetDriverInfoLevel;

	}

	@Override
	public void setStatus(DataResponse dataResponse, int status) {
		String message = null;
		switch (status) {
		case SystemCode.SUCCESS:
			message = SystemCode.SUCCESS_MESSAGE;
			break;
		case SystemCode.ERROR:
			message = SystemCode.ERROR_MESSAGE;
			break;
		case SystemCode.USER_INFO_NOT_EXIST_ERROR: // 用户信息不存在
			message = SystemCode.USER_INFO_NOT_EXIST_MESSAGE;
			break;
		case SystemCode.CANCEL_ORDER_TOO_MUCH: // 取消次数超过三次
			message = SystemCode.CANCEL_ORDER_MUCH_MESSAGE;
			break;
		case SystemCode.PUBLISH_ORDER_TOO_MUCH: // 接单次数超过三次
			message = SystemCode.PUBLISH_ORDER_TOO_MUCH_MESSAGE;
			break;
		case SystemCode.DATA_NULL_ERROR: // 请求数据为空
			message = SystemCode.DATA_NULL_MESSAGE;
			break;
		case SystemCode.COUPON_ACTIVATION_FAIL:
			message = SystemCode.COUPON_ACTIVATION_FAIL_MESSAGE;// 优惠券激活失败
			break;
		case SystemCode.STORE_LOAG_NULL:
			message = SystemCode.STORE_LOAD_NULL_MESSAGE;// 使用优惠券失败
			break;
		default:
			break;
		}
		dataResponse.setResponseCode(status);
		dataResponse.setResponseMessage(message);
	}

}
